@file:Suppress("DEPRECATION")

package com.csjbot.weedle_auto.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

import com.csjbot.weedle_auto.R

/**
 * author : chenqi.
 * e_mail : 1650699704@163.com.
 * create_time : 8/17/17.
 */

class DrawHookView : View {
    //绘制圆弧的进度值
    private var progress = 0
    //线1的x轴
    private var line1_x = 0
    //线1的y轴
    private var line1_y = 0
    //线2的x轴
    private var line2_x = 0
    //线2的y轴
    private var line2_y = 0

    private var mPaint: Paint? = null


    private var pass = true

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {}

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        onActionAnimotion(canvas)
    }

    fun init() {
        /**
         * 绘制圆弧
         */
        mPaint = Paint()
        //设置画笔颜色
        mPaint!!.color = resources.getColor(R.color.skyblue)
        //设置圆弧的宽度
        mPaint!!.strokeWidth = 5f
        //设置圆弧为空心
        mPaint!!.style = Paint.Style.STROKE
        //消除锯齿
        mPaint!!.isAntiAlias = true
    }

    fun onActionAnimotion(canvas: Canvas) {
        progress++//绘制进度增加
        //获取圆心的x坐标
        val center = width / 2
        val center1 = center - width / 5
        //圆弧半径
        val radius = width / 2 - 5

        //定义的圆弧的形状和大小的界限
        val rectF = RectF((center - radius - 1).toFloat(),
                (center - radius - 1).toFloat(), (center + radius + 1).toFloat(),
                (center + radius + 1).toFloat())

        //根据进度画圆弧
        canvas.drawArc(rectF, 235f, (-360 * progress / 100).toFloat(), false, mPaint!!)
        //先等圆弧画完，才话对勾
        if (progress >= 100) {
            if (pass) {
                if (line1_x < radius / 3) {
                    line1_x++
                    line1_y++
                }
                //画第一根线
                canvas.drawLine(center1.toFloat(), center.toFloat(), (center1 + line1_x).toFloat(), (center + line1_y).toFloat(), mPaint!!)

                if (line1_x == radius / 3) {
                    line2_x = line1_x
                    line2_y = line1_y
                    line1_x++
                    line1_y++
                }
                if (line1_x >= radius / 3 && line2_x <= radius) {
                    line2_x++
                    line2_y--
                }
                //画第二根线
                canvas.drawLine((center1 + line1_x - 1).toFloat(), (center + line1_y).toFloat(), (center1 + line2_x).toFloat(), (center + line2_y).toFloat(), mPaint!!)
            } else {
                progress = 0
            }
        }
        postInvalidateDelayed(10)
        //每隔10毫秒界面刷新
    }


    override fun onDrawForeground(canvas: Canvas) {
        super.onDrawForeground(canvas)
    }
}
